package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ClientUser;
import com.ruoyi.system.domain.TmcMaintenanceAttach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TmcPropertyMaintenanceMapper;
import com.ruoyi.system.domain.TmcPropertyMaintenance;
import com.ruoyi.system.service.ITmcPropertyMaintenanceService;

/**
 * 维修记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@Service
public class TmcPropertyMaintenanceServiceImpl implements ITmcPropertyMaintenanceService {
    @Autowired
    private TmcPropertyMaintenanceMapper tmcPropertyMaintenanceMapper;

    /**
     * 查询维修记录
     *
     * @param id 维修记录ID
     * @return 维修记录
     */
    @Override
    public TmcPropertyMaintenance selectTmcPropertyMaintenanceID(Long id) {
        TmcPropertyMaintenance tmcPropertyMaintenance = tmcPropertyMaintenanceMapper.selectTmcPropertyMaintenanceID(id);
        tmcPropertyMaintenance.setCompleteAttachList(tmcPropertyMaintenanceMapper.selectTmcMaintenanceByIds(id));
        List<TmcMaintenanceAttach> tmcMaintenanceAttaches = tmcPropertyMaintenanceMapper.selectTmcMaintenanceById(id);
        tmcPropertyMaintenance.setAttachList(tmcMaintenanceAttaches);
        return tmcPropertyMaintenance;
    }

    @Override
    public TmcPropertyMaintenance selectTmcPropertyMaintenanceById(Long id) {
        return tmcPropertyMaintenanceMapper.selectTmcPropertyMaintenanceById(id);
    }

    /**
     * 查询维修记录列表
     *
     * @param tmcPropertyMaintenance 维修记录
     * @return 维修记录
     */
    @Override
    public List<TmcPropertyMaintenance> selectTmcPropertyMaintenanceList(TmcPropertyMaintenance tmcPropertyMaintenance) {
        return tmcPropertyMaintenanceMapper.selectTmcPropertyMaintenanceList(tmcPropertyMaintenance);
    }

    /**
     * 新增维修记录
     *
     * @param tmcPropertyMaintenance 维修记录
     * @return 结果
     */
    @Override
    public int insertTmcPropertyMaintenance(TmcPropertyMaintenance tmcPropertyMaintenance) {
        List<ClientUser> clientUsers = tmcPropertyMaintenanceMapper.queryCompany_ID(tmcPropertyMaintenance.getHouseinfoId());
        int math = (int) (Math.random() * clientUsers.size());
        tmcPropertyMaintenance.setClientId(clientUsers.get(math).getId());
        tmcPropertyMaintenance.setCreateTime(DateUtils.getNowDate());
        return tmcPropertyMaintenanceMapper.insertTmcPropertyMaintenance(tmcPropertyMaintenance);
    }

    @Override
    public int insertTmcMaintenanceAttach(TmcMaintenanceAttach tmcMaintenanceAttach) {
        tmcMaintenanceAttach.setCreateTime(DateUtils.getNowDate());
        return tmcPropertyMaintenanceMapper.insertTmcMaintenanceAttach(tmcMaintenanceAttach);
    }

    /**
     * 修改维修记录
     *
     * @param tmcPropertyMaintenance 维修记录
     * @return 结果
     */
    @Override
    public int updateTmcPropertyMaintenance(TmcPropertyMaintenance tmcPropertyMaintenance) {
        return tmcPropertyMaintenanceMapper.updateTmcPropertyMaintenance(tmcPropertyMaintenance);
    }

    /**
     * 批量删除维修记录
     *
     * @param ids 需要删除的维修记录ID
     * @return 结果
     */
    @Override
    public int deleteTmcPropertyMaintenanceByIds(Long[] ids) {
        return tmcPropertyMaintenanceMapper.deleteTmcPropertyMaintenanceByIds(ids);
    }

    /**
     * 删除维修记录信息
     *
     * @param id 维修记录ID
     * @return 结果
     */
    @Override
    public int deleteTmcPropertyMaintenanceById(Long id) {
        return tmcPropertyMaintenanceMapper.deleteTmcPropertyMaintenanceById(id);
    }

    @Override
    public List<TmcMaintenanceAttach> selectAttachById(Long ID) {
        return tmcPropertyMaintenanceMapper.selectAttachById(ID);
    }

    @Override
    public List<TmcPropertyMaintenance> QueryUserMaintenanceList(TmcPropertyMaintenance tmcPropertyMaintenance) {
        List<TmcPropertyMaintenance> tmcPropertyMaintenances = tmcPropertyMaintenanceMapper.selectTmcPropertyMaintenanceById(tmcPropertyMaintenance);
        for (int i = 0; i < tmcPropertyMaintenances.size(); i++) {
            tmcPropertyMaintenances.get(i).setAttachList(tmcPropertyMaintenanceMapper.selectTmcMaintenanceById(tmcPropertyMaintenances.get(i).getId()));
            tmcPropertyMaintenances.get(i).setCompleteAttachList(tmcPropertyMaintenanceMapper.selectTmcMaintenanceByIds(tmcPropertyMaintenances.get(i).getId()));
        }
        return tmcPropertyMaintenances;
    }

    @Override
    public List<TmcPropertyMaintenance> QueryUserMaintenanceListByID(TmcPropertyMaintenance tmcPropertyMaintenance) {
        List<TmcPropertyMaintenance> tmcPropertyMaintenances = tmcPropertyMaintenanceMapper.selectTmcPropertyMaintenanceByCId(tmcPropertyMaintenance);
        for (int i = 0; i < tmcPropertyMaintenances.size(); i++) {
            tmcPropertyMaintenances.get(i).setAttachList(tmcPropertyMaintenanceMapper.selectTmcMaintenanceById(tmcPropertyMaintenances.get(i).getId()));
            tmcPropertyMaintenances.get(i).setCompleteAttachList(tmcPropertyMaintenanceMapper.selectTmcMaintenanceByIds(tmcPropertyMaintenances.get(i).getId()));
        }
        return tmcPropertyMaintenances;
    }

    @Override
    public int CompleteMaintenance(Long maintenanceID) {
        return tmcPropertyMaintenanceMapper.CompleteMaintenance(maintenanceID);
    }
}
